package com.jsmile.cloud.msgcenter.pay.feign;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jsmile.cloud.msgcenter.api.req.ReqPayOrderCreate;
import com.jsmile.cloud.msgcenter.api.req.ReqPayOrderRefund;
import com.jsmile.cloud.msgcenter.pay.PayOrderApplicationService;
import com.jsmile.cloud.tradecenter.api.req.ReqPayOrder;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.log.annotation.JsmileLog;

import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@AllArgsConstructor
@Api(tags = "订单支付管理")
public class PayOrderServiceApiImpl extends AuthcController {

    private final PayOrderApplicationService payOrderApplicationService;

    @RequestMapping(value = "/feign/payOrder/create", method = RequestMethod.POST)
    @JsmileLog(title = "消息中心->创建支付订单", type = LogTypeEnum.INSERT, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult create(@RequestBody @Valid ReqPayOrderCreate reqPayOrderCreate, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqPayOrderCreate.setCurUserId(jsmileContext.getUserId());
        reqPayOrderCreate.setCurTenantId(jsmileContext.getTenantId());
        reqPayOrderCreate.setIp(jsmileContext.getIp());
        payOrderApplicationService.create(reqPayOrderCreate);
        return JSmileResult.success();
    }

    @RequestMapping(value = "/feign/payOrder/toPay", method = RequestMethod.POST)
    @JsmileLog(title = "消息中心->去支付", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult toPay(@RequestBody @Valid ReqPayOrder reqPayOrder, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.TRADE_CENTER);
        reqPayOrder.setCurUserId(jsmileContext.getUserId());
        reqPayOrder.setCurTenantId(jsmileContext.getTenantId());
        return JSmileResult.success(payOrderApplicationService.pay(reqPayOrder));
    }

    @RequestMapping(value = "/feign/payOrder/refund", method = RequestMethod.POST)
    @JsmileLog(title = "消息中心->退款", type = LogTypeEnum.UPDATE, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult refund(@RequestBody @Valid ReqPayOrderRefund reqPayOrderRefund, @CurrentContext JsmileContext jsmileContext) {
        this.initContext(jsmileContext, JSmileConstant.SystemId.MSG_CENTER);
        reqPayOrderRefund.setCurUserId(jsmileContext.getUserId());
        reqPayOrderRefund.setCurTenantId(jsmileContext.getTenantId());
        payOrderApplicationService.refund(reqPayOrderRefund);
        return JSmileResult.success();
    }
}
